#ifndef TWOCOLUMNDELEGATE_H
#define TWOCOLUMNDELEGATE_H

#include <QStyledItemDelegate>
#include <QListView>
#include <QStringListModel>
#include <QStyledItemDelegate>
#include <QPainter>
#include <QVBoxLayout>
#include <QWidget>
class TwoColumnDelegate : public QStyledItemDelegate
{
public:
    explicit TwoColumnDelegate(QObject *parent = nullptr);
    // TwoColumnDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) {}

    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override {
        if (index.isValid()) {
            QStyleOptionViewItem opt = option;
            initStyleOption(&opt, index);

            // 分割数据为两列
            QStringList columns = index.data(Qt::DisplayRole).toString().split("|");
            if (columns.size() == 2) {
                painter->save();
                painter->setRenderHint(QPainter::Antialiasing);
                painter->setPen(opt.palette.color(QPalette::Text));

                // 计算列宽
                int width = option.rect.width() / 2;
                QRect leftRect(option.rect.x(), option.rect.y(), width, option.rect.height());
                QRect rightRect(option.rect.x() + width, option.rect.y(), width, option.rect.height());

                // 绘制两列数据
                painter->drawText(leftRect, Qt::AlignLeft | Qt::AlignVCenter, columns[0].trimmed());
                painter->drawText(rightRect, Qt::AlignLeft | Qt::AlignVCenter, columns[1].trimmed());

                painter->restore();
            } else {
                QStyledItemDelegate::paint(painter, option, index);
            }
        }
    }

    QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override {
        QSize size = QStyledItemDelegate::sizeHint(option, index);
        size.setHeight(20); // 设置行高
        return size;
    }
};

#endif // TWOCOLUMNDELEGATE_H
